var app = getApp();
var listArr = [];

Page({
  data: {
    feedId: '',
    isLoading: false,
    isPopUp: true,
    questionList: [],
    isDisabled: false
  },
  onLoad: function(opt) {
    var that = this;
    this.setData({
      feedId: opt.feedId
    })
    wx.showLoading({
      title: '加载中',
    });
    this.getQuestionnaire(); // 获取问卷
  },
  radioChange: function(e) {
    var questionId = e.currentTarget.dataset.questionid;
    var index = e.currentTarget.dataset.index;
    var answer = e.detail.value;
    listArr[index] = {
      questionId,
      answer
    }
  },
  inputBlur: function(e) {
    var questionId = e.currentTarget.dataset.questionid;
    var index = e.currentTarget.dataset.index;
    var answer = e.detail.value;
    listArr[index] = {
      questionId,
      answer
    }
  },
  // 提交表单-问卷调查
  formSubmitInv: function(e) {
    var that = this;
    var formObj = e.detail.value;

    var that = this;
    if (listArr.length < this.data.questionList.length) {
      wx.showToast({
        title: '请完善题目',
        icon: 'none',
        duration: 2000
      })
      return false
    } else {
      that.setData({
        isDisabled: true
      });
      wx.request({
        url: app.globalData.apiBaseUrl + '/wechat/submitQuestionInfo',
        header: {
          'content-type': 'application/x-www-form-urlencoded',
          'FilterToken_MT': wx.getStorageSync("token")
        },
        method: 'POST',
        dataType: 'json',
        data: {
          paperId: formObj.paperId,
          feedId: that.data.feedId,
          list: JSON.stringify(listArr)
        },
        success: function(res) {
          res = res.data;
          if (res.code == 200) {
            that.setData({
              score: res.data,
              isPopUp: false
            })
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 3000
            });
            that.setData({
              isDisabled: false
            })
          }
        }
      })
    }
  },
  // 获取基础问卷
  getQuestionnaire: function() {
    var that = this;
    wx.request({
      url: app.globalData.apiBaseUrl + '/wechat/getChildrenPhbqQuestionInfo',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'FilterToken_MT': wx.getStorageSync("token")
      },
      method: 'GET',
      dataType: 'json',
      success: function(res) {
        res = res.data;
        if (res.code == 200) {
          that.setData({
            questionList: res.data.medicalQuestionInfoList,
            isLoading: true
          })
          wx.hideLoading();
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 3000
          })
        }
      }
    })
  },
  // 关闭弹窗
  closePopUp: function () {
    this.setData({
      isPopUp: true
    })
    wx.navigateBack({
      delta: 3
    })
  }
})